package com.a2.appa3.util;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class Timeutils {
    private static String time;
    private static SimpleDateFormat mSimpleDateFormat = null;

    //倒计时
    public void startTime(final String startTime, final String nowTime) {
        final Timer timer = new Timer();
        final Date today = new Date();

        TimerTask timerTask = new TimerTask() {
            String mStartTile = startTime;
            String mNowTile = nowTime;

            @Override
            public void run() {
                Date startDate = DateUtils.strToDateLong(mStartTile);
                Date nowDate = DateUtils.strToDateLong(mNowTile);
                long diff = startDate.getTime() - nowDate.getTime();
                int downtimeInt = (int) diff / 1000;
                if (downtimeInt < 3_600 && downtimeInt != 0) {
                    if (downtimeInt < 60) {
                        if (downtimeInt == 0)
                            Log.i("TAG", "倒计时结束");
                        else
                            Log.i("TAG", String.valueOf(downtimeInt) + "秒后" + "结束");
                    } else {
                        int mimute = downtimeInt / 60;
                        int second = downtimeInt % 60;
                        Log.i("TAG", String.valueOf(mimute) + "分" + String.valueOf(second) + "秒后结束");
                    }
                } else if (downtimeInt == 0) {
                    timer.cancel();
                } else {
                    //当天
                    if (today.getYear() == startDate.getYear() && today.getMonth() == startDate.getMonth() && today.getDate() == startDate.getDate()) {
//                        Log.i("TAG", MyUtils.simpleDateStringHm(startDate) + "\n结束");
                    } else { //大于一天
//                        Log.i("TAG", MyUtils.simpleDateStringMDHM(startDate) + "\n结束 ");
                    }
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(DateUtils.strToDateLong(mNowTile));
                calendar.add(Calendar.SECOND, 1);
                mNowTile = DateUtils.formatDate(calendar.getTime());
            }
        };
        timer.schedule(timerTask, 0, 1000);
    }

    /*时间戳转换成字符窜*/
    public static String getorderDateToString(long time) {
        Date d = new Date(time);

        mSimpleDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return mSimpleDateFormat.format(d);
    }

//    /*时间戳转换成字符窜*/
//    public static String getorderDateToString(long time) {
//        Date d = new Date(time);
//
//        mSimpleDateFormat = new SimpleDateFormat("yyyy-M-dd     HH:mm:ss", Locale.getDefault());
//        return mSimpleDateFormat.format(d);
//    }

    /*将字符串转为时间戳*/
    public static long getStringToDate(String time) {
        mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date date = new Date();
        try {
            date = mSimpleDateFormat.parse(time);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

}
